package net.homelinux.chaoswg.io.renga.krypto.animations.Subtract;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;

public class SubtractAnimation extends Animation {
    SubtractState initialState;
    SubtractStateChanger subtractChanger;

    public SubtractAnimation () {
        initialize(new SubtractState());
    }

    public SubtractAnimation (SubtractState initialState) {
        initialize(initialState);
    }

    private void initialize(State initialState) {
        if (!(initialState instanceof SubtractState)) {
            throw new IllegalArgumentException ();
        }
        this.initialState = ((SubtractState)initialState);
        this.subtractChanger = new SubtractStateChanger (this.initialState);
    }

    @Override
    public synchronized void start() {
        Thread t = new Thread (subtractChanger);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        subtractChanger.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }

    @Override
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }

}
